package com.csgc.camera.widget.dialog

import android.app.Dialog
import android.os.Bundle
import android.view.View
import android.widget.FrameLayout
import android.widget.LinearLayout
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import androidx.viewpager.widget.ViewPager
import com.csgc.camera.bean.MaskDataInfo
import com.csgc.camera.constants.MaskConfig
import com.csgc.camera.events.ChangeMaskEvent
import com.csgc.camera.storage.MaskPreference
import com.csgc.camera.util.CommonUtils
import com.csgc.camera.util.clickWithTrigger
import com.csgc.camera.widget.dialog.adapter.MaskPageAdapter
import com.csgc.camera.widget.dialog.adapter.MaskTabAdapter
import com.csgc.camera.widget.dialog.base.BottomDialog
import com.cysd.spsyxj.R
import com.drake.channel.receiveEvent
import com.hlyj.http.ad.NewGroMoreFeedAdManager
import com.hnyyac.ad.mds.utils.BaseAdConstants
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.Moshi
import com.squareup.moshi.Types

class MaskChooseDialog : DialogFragment() {

    private var mDialog: BottomDialog? = null
    private var viewPager: ViewPager? = null
    private var maskContainer: LinearLayout? = null
    private var frameLayout: FrameLayout? = null
    private var maskType = 0
    private var maskId = 0

    private val jsonMaskAdapter: JsonAdapter<List<MaskDataInfo>> by lazy {
        val moshi = Moshi.Builder().build()
        val infoType = Types.newParameterizedType(List::class.java, MaskDataInfo::class.java)
        val jsonAdapter: JsonAdapter<List<MaskDataInfo>> = moshi.adapter(infoType)
        jsonAdapter
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        maskType = MaskPreference.instance.getMaskType()
        maskId = MaskPreference.instance.getMaskId()
        receiveEvent<ChangeMaskEvent> {
            maskType = it.type
            maskId = it.id
            maskContainer?.removeAllViews()

            context?.let { tempContext ->
                val maskView = MaskConfig.getMaskView(tempContext, it.type, it.id)
                maskContainer?.addView(maskView)
            }
        }
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val json = CommonUtils.getAssetsJson("mask_data.json", requireContext())
        val originalList: List<MaskDataInfo> = jsonMaskAdapter.fromJson(json) ?: ArrayList()
        val mergedMaskItemList = originalList.flatMap { it.list }
        val singleMaskDataInfo = MaskDataInfo("全部水印", 0, mergedMaskItemList)
        val newList = listOf(singleMaskDataInfo)


        mDialog = BottomDialog(requireContext()).apply {
            setCustomView(R.layout.dialog_mask_choose)
            findViewById<View>(R.id.iv_sure)?.clickWithTrigger { dismissAllowingStateLoss() }

            maskContainer = findViewById(R.id.layout_mask_container)
            frameLayout = findViewById(R.id.frame_content)
            //根据mask type 和id 来添加不同的水印
            val type = MaskPreference.instance.getMaskType()
            val id = MaskPreference.instance.getMaskId()

            val maskView = MaskConfig.getMaskView(context, type, id)
            maskContainer?.addView(maskView)



            viewPager = findViewById(R.id.vp_mask_page)
            viewPager?.offscreenPageLimit = 1
            viewPager?.adapter = MaskPageAdapter(newList, childFragmentManager)
            viewPager?.currentItem = type

            val adapter = MaskTabAdapter(requireContext(), ArrayList(), viewPager!!)

//                setCancelable(false)
//                setCanceledOnTouchOutside(false)
            val newGromoreFeedAd: NewGroMoreFeedAdManager = NewGroMoreFeedAdManager(
                requireActivity(),
                BaseAdConstants.FEEDAD_Id, frameLayout, 66
            )
            newGromoreFeedAd.loadAd()
        }


        return mDialog!!
    }


    fun showDialog(manager: FragmentManager, tag: String) {
        show(manager, tag)
    }


}